package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanFenZuSavePO;
import com.ct.application.shipping.model.po.ChuanYuanFenZuSearchPO;
import com.ct.application.shipping.model.vo.ChuanBoXinXiVO;
import com.ct.application.shipping.model.vo.ChuanYuanFenZuVO;
import com.ct.application.shipping.service.ChuanYuanFenZuService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanFenZuMapper;
import com.ct.shipping.orm.ChuanBoXinXiDO;
import com.ct.shipping.orm.ChuanBoZhiWeiDO;
import com.ct.shipping.orm.ChuanYuanFenZuDO;
import com.ct.shipping.orm.ChuanYuanFenZuDO;
import com.ct.utils.result.StatusEnums;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 船员分组 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanFenZuServiceImpl extends BaseServiceImpl<ChuanYuanFenZuMapper, ChuanYuanFenZuDO>
        implements ChuanYuanFenZuService {

    /**
     * 查询分组分页列表
     */
    @Override
    public List<ChuanYuanFenZuVO> findChuanYuanFenZu() {
        List<ChuanYuanFenZuDO> chuanYuanFenzus = this.baseMapper.findChuanYuanFenzus();
        return tranferListBo(chuanYuanFenzus, ChuanYuanFenZuVO.class);
    }


    @Override
    public List<ChuanYuanFenZuVO> findChuanYuanFenZuList(){
        LambdaQueryWrapper<ChuanYuanFenZuDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(ChuanYuanFenZuDO::getCreateTime);
        return tranferListBo(this.list(lambdaQueryWrapper),ChuanYuanFenZuVO.class);
    }


    /**
     * 保存分组
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveChuanYuanFenZu(ChuanYuanFenZuSavePO savePO){
        savePO.setId(null); //新增不需要id

        ChuanYuanFenZuDO chuanYuanFenZuDO = this.lambdaQuery()
                .eq(ChuanYuanFenZuDO::getZuMing, savePO.getZuMing())
                .one();
        if (chuanYuanFenZuDO != null) {
            throw new RuntimeException("《"+savePO.getZuMing()+"》组名已存在，请更换");
        }


        ChuanYuanFenZuDO chuanYuanFenZuWeiDO = tranferBo(savePO, ChuanYuanFenZuDO.class);
        if (Objects.nonNull(chuanYuanFenZuWeiDO)){
            chuanYuanFenZuWeiDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanYuanFenZuWeiDO.setSystemName(UserThreadLocal.get().getSystemName());
            return this.save(chuanYuanFenZuWeiDO);
        }
        return false;
    }

    /**
     * 修改分组
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChuanYuanFenZu(ChuanYuanFenZuSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        ChuanYuanFenZuDO chuanYuanFenZuDO = this.lambdaQuery()
                .eq(ChuanYuanFenZuDO::getZuMing, savePO.getZuMing())
                .ne(ChuanYuanFenZuDO::getId, savePO.getId())
                .one();
        if (chuanYuanFenZuDO != null) {
            throw new RuntimeException("《"+savePO.getZuMing()+"》组名已存在，请更换");
        }

        ChuanYuanFenZuDO chuanYuanFenZuWeiDO = tranferBo(savePO, ChuanYuanFenZuDO.class);
        if (Objects.nonNull(chuanYuanFenZuWeiDO)){
            return this.updateById(chuanYuanFenZuWeiDO);
        }
        return false;
    }


    /**
     * 删除分组
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanYuanFenZu(Long id){
        return this.removeById(id);
    }


    /**
     * 分组状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanYuanFenZuVO getChuanYuanFenZu(Long id){
        ChuanYuanFenZuDO chuanYuanFenZuWeiDO = getById(id);
        return tranferBo(chuanYuanFenZuWeiDO,ChuanYuanFenZuVO.class);
    }
}
